import Request from '../js_sdk/luch-request/luch-request'
import  { BASE_URL }  from './BASE_URL.js'
import { jumpPage } from '@/common/hooks.js'
import store from '@/store/index.js'
/**
 * @description 请求基础配置
 */
const http = new Request({
//   baseURL: BASE_URL, //根域名
  baseURL: process.env.NODE_ENV === "development" ? '/api' : BASE_URL+'api',
	header: {
		'Content-Type' :'application/json'
	}
});

/**
 * @description 请求拦截器
 */
// http.interceptors.request.BASE_URL = '/api'
http.interceptors.request.use((config) => {
	uni.showLoading({
		title:"加载中..."
	})
	config.data = config.data || {}
	 config.header['token'] = store.state.token
	// console.log(config)
	return config
}, config => {
	return Promise.reject(config)
})

/**
 * @description 响应拦截器
 */
http.interceptors.response.use((res) => {


	uni.hideLoading()
	if ( res.data.code !== 200 ) {
		return uni.showToast({title:  res.data.msg,icon: 'none',duration: 1000})
	}
	if ( res.data.code == 401  ) {
		uni.showToast({title:  '鉴权过期请您重新登录',icon: 'none',duration: 1000})
		setTimeout(()=>{
			store.commit('clearCacheData')
			jumpPage('/pages/login/login')
		},300)
	}
	// if ( res.data.status == '-3'  ) {
	// 	setTimeout(()=>{
	// 		store.commit('clearCacheData')
	// 		jumpPage('/pages/login/login')
	// 	},400)
	// }
	// if ( res.data.status == '-4'  ) {
	// 	setTimeout(()=>{
	// 		store.commit('clearCacheData')
	// 		jumpPage('/pages/login/login')
	// 	},400)
	// }
	// if ( res.data.status == '-5'  ) {
	// 	setTimeout(()=>{
	// 		store.commit('clearCacheData')
	// 		jumpPage('/pages/login/login')
	// 	},400)
	// }

	return  res 
}, (response) => {
	if (response.data.code == 401) {
		uni.showToast({title:  '鉴权过期请您重新登录',icon: 'none',duration: 1000})
		setTimeout(()=>{
			store.commit('clearCacheData')
			jumpPage('/pages/login/login')
		},300)
	}
	// 对响应错误做点什么 （statusCode !== 200）
	return Promise.reject(response)
})

export default http